
package com.syh.niceweibo.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.syh.niceweibo.common.Constants;
import com.syh.niceweibo.common.LogUtils;


public class DBadapter extends SQLiteOpenHelper {

    private static final String TAG = "DBadapter";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "nice_weibo";

    
    private final static String CREATE_TABLE_HOT_WEIBO = "create table "
            + Constants.TABLE_HOT_WEIBO
            + " (_id integer primary key autoincrement,weiboId integer,content text,original_pic text,bmiddle_pic text,created_at integer,uid integer, screen_name text, profile_image_url text);";
    private final static String CREATE_TABLE_HOT_WEIBO_INDEX="CREATE INDEX WEIBOS_ID_INDEX ON "+Constants.TABLE_HOT_WEIBO+"(weiboId);";
    
    private SQLiteDatabase db;

    public DBadapter(Context context) {
        super(context, DATABASE_NAME, null,
                DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.beginTransaction();
        try {
        	db.execSQL(CREATE_TABLE_HOT_WEIBO);
            db.execSQL(CREATE_TABLE_HOT_WEIBO_INDEX);
            db.setTransactionSuccessful();
        } catch (SQLException e) {
        	e.printStackTrace();
            if(LogUtils.DEBUG)LogUtils.e(TAG, "db onCreate e"+e.toString());
        } finally {
            db.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.beginTransaction();
        try {
            db.execSQL("DROP TABLE IF EXISTS " + Constants.TABLE_HOT_WEIBO);
            onCreate(db);
            db.setTransactionSuccessful();
        } catch (SQLException e) {
        	e.printStackTrace();
            if(LogUtils.DEBUG)LogUtils.e(TAG, "db onUpgrade e"+e.toString());
        } finally {
            db.endTransaction();
        }

    }

    public synchronized DBadapter open() throws SQLException {
        db = this.getWritableDatabase();
        return this;
    }

    public synchronized DBadapter openReadable() throws SQLException {
        db = this.getReadableDatabase();
        return this;
    }

    public synchronized void close() {
        if (null != db) {
            db.close();
        }
    }

    public Cursor rawQuery(String sql, String[] selectionArgs) {
        return db.rawQuery(sql, selectionArgs);
    }

    public boolean delete(String table, String whereClause, String[] whereArgs) {
        return db.delete(table, whereClause, null) > 0;
    }

    public long insert(String table, String nullColumnHack, ContentValues cv) {
        return db.insert(table, nullColumnHack, cv);
    }

    public int update(String table, ContentValues cv, String whereClause,
            String[] whereArgs) {
        return db.update(table, cv, whereClause, whereArgs);
    }
}
